using Hydrogen.ULib.Attributes;
using System;
using UnityEngine;

namespace Hydrogen.ULib.UINavigation
{
    [DisallowMultipleComponent]
    public abstract class UINavSelectable : MonoBehaviour, IUINavSelectable
    {
        [Header(nameof(UINavSelectable))]
        [SerializeField]
        private Arrow _movableArrow = Arrow.All;
        public Arrow MoveableArrow
        {
            get => _movableArrow;
            set => _movableArrow = value;
        }

        public Rect Rect => RectTransform.rect;

        public float XMax => Pos.x + RectTransform.rect.width * (1 - RectTransform.pivot.x);
        public float XMin => Pos.x - RectTransform.rect.width * RectTransform.pivot.x;
        public float YMax => Pos.y + RectTransform.rect.height * (1 - RectTransform.pivot.y);
        public float YMin => Pos.y - RectTransform.rect.height * RectTransform.pivot.y;

        private Vector2 Pos
        {
            get
            {
                this.m_pos = UINavUtils.PosFromRectTrsToOtherRectTrs(RectTransform.position, _canvasRT, _uiCamera);
                return this.m_pos;
            }
        }
        [SerializeField, ReadOnly]
        private Vector2 m_pos;

        private Canvas _canvas
        {
            get
            {
                if (!m_canvas) m_canvas = GetComponentInParent<Canvas>();
                return m_canvas;
            }
        }
        private Canvas m_canvas;


        private RectTransform _canvasRT
        {
            get
            {
                if (!m_canvasRT) m_canvasRT = _canvas.GetComponent<RectTransform>();
                return m_canvasRT;
            }
        }
        private RectTransform m_canvasRT;




        private Camera _uiCamera
        {
            get
            {
                if (!m_uiCamera) m_uiCamera = _canvas.worldCamera;
                return m_uiCamera;
            }
        }
        private Camera m_uiCamera;

        private void Start()
        {
            var p = Pos;
        }

        public virtual bool Selectable
        {
            get
            {
                return gameObject.activeSelf && this._selectable;
            }
        }
        [SerializeField]
        private bool _selectable = true;

        public Vector2 Position => RectTransform.anchoredPosition;

        public RectTransform RectTransform
        {
            get
            {
                if (!m_rectTransform) m_rectTransform = GetComponent<RectTransform>();
                return m_rectTransform;
            }
        }
        private RectTransform m_rectTransform;

        public virtual bool CanMoveNext(Arrow arrow)
        {
            return (MoveableArrow & arrow) == arrow;
        }

        public void OnMoved(Arrow arrow, IUINavSelectable next)
        {
            UINavUtils.ForeachComponents<IUINavNodeMovedHandle>(transform, t => t.OnMoved(arrow, next));
        }

        public virtual void Selected()
        {

            UINavUtils.ForeachComponents<IUINavNodeSelectedHandle>(transform, t => t.OnSelectedHandle());
        }

        public virtual void Unselected()
        {
            UINavUtils.ForeachComponents<IUINavNodeUnselectedHandle>(transform, t => t.OnUnselectedHandle());
        }

        public virtual void Submited(IUINavHandle group)
        {
            UINavUtils.ForeachComponents<IUINavNodeSubmitedHandle>(transform, t => t.OnSubmitedHandle());
        }
    }
}
